/*
 * Copyright (c) 2006 - 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.io.*;
import java.util.Arrays;
import java.util.Comparator;

/**
 * <p>Project: app2022 - Exam07
 * <p>Powered by webrx On 2022-01-20 10:41:32
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Exam07 {
    public static void main(String[] args) throws IOException {

        File srcFile = new File("p2.jpg");
        //拆分文件
        judge(srcFile, 5);
        //合并文件
        //merge(new File("p2.jpg.1"), new File("p2.jpg.0"), new File("p2.jpg.2"), new File("p2.jpg.3"), new File("p2.jpg.4"));
    }

    public static void merge(File... file) throws IOException {
        //Pattern compile = Pattern.compile("(.*)\\.(.*)");
        for (File f : file) {
            if (f.isDirectory()) {
                return;
            }
        }
        Arrays.sort(file, new Comparator<File>() {
            @Override
            public int compare(File o1, File o2) {
                int i = Integer.parseInt(o1.getName().substring(o1.getName().lastIndexOf(".") + 1));
                int j = Integer.parseInt(o2.getName().substring(o2.getName().lastIndexOf(".") + 1));
                return i - j;
            }
        });

        File file1 = new File(file[0].getParentFile(), file[0].getName().substring(0, file[0].getName().lastIndexOf(".")));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file1));
        BufferedInputStream bis = null;
        for (File f : file) {
            bis = new BufferedInputStream(new FileInputStream(f));
            bos.write(bis.readAllBytes());
            bis.close();
            f.delete();
        }
        bos.close();
    }

    public static void judge(File srcFile, int x) throws IOException {
        if (srcFile.exists() && srcFile.isFile()) {
            File childDir = new File(new File(srcFile.getAbsolutePath()).getParent());
            spit(srcFile, childDir, x);
        } else {
            System.out.printf("%s 不是文件无法操作！", srcFile);
        }

    }

    public static void spit(File srcFile, File childDir, int x) throws IOException {
        long length = srcFile.length();
        //默认分为3个文件
        if (x <= -1) {
            x = 3;
        }
        long[] childLength = new long[x];
        long cLength = length / x;
        if ((cLength = length / x) == 0) {
            for (int i = 0; i < x; i++) {
                childLength[i] = cLength;
            }
        } else {
            for (int i = 0; i < x - 1; i++) {
                childLength[i] = cLength;
            }
            childLength[x - 1] = cLength + length % x;
        }
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
        BufferedOutputStream bos = null;
        byte[] bytearr = bis.readAllBytes();
        int num = 0;
        for (int i = 0; i < x; i++) {
            File file = new File(childDir, srcFile.getName() + "." + i);
            bos = new BufferedOutputStream(new FileOutputStream(file));
            bos.write(bytearr, num, (int) childLength[i]);
            num += (int) childLength[i];
            bos.flush();
            bos.close();
        }
        bos.close();
        bis.close();
    }
}
